package com.crm.service.impl;

import com.crm.context.ThreadLocalUtil;
import com.crm.mapper.BusinessMapper;
import com.crm.mapper.ClueMapper;
import com.crm.mapper.IndexMapper;
import com.crm.service.IndexService;
import com.crm.vo.IndexBaseInfoVO;
import com.crm.vo.IndexStatisticsVo;
import com.crm.vo.IndexTodayInfoVO;
import com.crm.vo.IndexTodoInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private IndexMapper indexMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private ClueMapper clueMapper;

    @Override
    public IndexBaseInfoVO getBaseInfo(LocalDate beginCreateTime, LocalDate endCreateTime) {
        Long id = ThreadLocalUtil.getCurrentId();
        String username=indexMapper.getUserName(id);
        username=username==null?"lisi":username;
        Integer businessNum=indexMapper.countBusinessNum(username,beginCreateTime,endCreateTime);
        businessNum =businessNum==null?0:businessNum;
        Integer cluesNum=indexMapper.countCluesNum(username,beginCreateTime,endCreateTime);
        cluesNum=cluesNum==null?0:cluesNum;
        Integer contractNum=indexMapper.countContractNum(username,beginCreateTime,endCreateTime);
        contractNum=contractNum==null?0:cluesNum;
        Double salesAmount=indexMapper.sumAmount(username,beginCreateTime,endCreateTime);
        if (salesAmount==null){
            salesAmount=0.0;
        }
        return IndexBaseInfoVO.builder()
                .businessNum(businessNum)
                .cluesNum(cluesNum)
                .contractNum(contractNum)
                .salesAmount(salesAmount)
                .build();

    }

    @Override
    public IndexTodayInfoVO getTodayInfo() {

        Long id = ThreadLocalUtil.getCurrentId();
        String username=indexMapper.getUserName(id);
        return IndexTodayInfoVO.builder()
                .todayBusinessNum(indexMapper.todayBusinessNum(LocalDate.now(),username))
                .todayCluesNum(indexMapper.todayCluesNum(LocalDate.now(),username))
                .todayContractNum(indexMapper.todayContractNum(LocalDate.now(),username))
                .todaySalesAmount(indexMapper.salesAmount(LocalDate.now(),username))
                .build();
    }

    @Override
    public IndexTodoInfoVO getTodoInfo(LocalDate beginCreateTime, LocalDate endCreateTime) {
        Long id = ThreadLocalUtil.getCurrentId();
        String username=indexMapper.getUserName(id);
        Integer tofollowedCluesNum=indexMapper.countTofollowedCluesNum(username,beginCreateTime,endCreateTime);
        Integer tofollowedBusinessNum=indexMapper.countTofollowedBusinessNum(username,beginCreateTime,endCreateTime);
        Integer toallocatedCluesNum=indexMapper.countToallocatedCluesNum(username,beginCreateTime,endCreateTime);
        Integer toallocatedBusinessNum=indexMapper.countToallocatedBusinessNum(username,beginCreateTime,endCreateTime);
        return IndexTodoInfoVO.builder()
                .toallocatedBusinessNum(toallocatedBusinessNum)
                .toallocatedCluesNum(toallocatedCluesNum)
                .tofollowedBusinessNum(tofollowedBusinessNum)
                .tofollowedCluesNum(tofollowedCluesNum)
                .build();
    }

    @Override
    public List<Map<String, Object>> businessChangeStatistics(IndexStatisticsVo indexStatisticsVo) {
        Integer allBusiness=businessMapper.countAllBusiness(indexStatisticsVo.getBeginCreateTime(),indexStatisticsVo.getEndCreateTime());
        List<Map<String,Object>> list=businessMapper.countAllContractByUser(indexStatisticsVo);
        for (Map<String, Object> map : list) {
            Long num =(Long) map.get("num");
            map.put("radio",getRadio(allBusiness,num));
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> salesStatistic(IndexStatisticsVo indexStatisticsVo) {
        Integer allClue=clueMapper.countAllClue(indexStatisticsVo.getBeginCreateTime(),indexStatisticsVo.getEndCreateTime());
        List<Map<String,Object>> list=clueMapper.countAllClueByUser(indexStatisticsVo);
        for (Map<String, Object> map : list) {
            Long num =(Long) map.get("num");
            map.put("radio",getRadio(allClue,num));
        }
        return list;
    }

    private BigDecimal getRadio(Integer all,Long num){
        if(all.intValue()==0){
            return new BigDecimal(0);
        }
        BigDecimal numBigDecimal = new BigDecimal(num);
        BigDecimal allBigDecimal = new BigDecimal(all);
        BigDecimal divide = numBigDecimal.divide(allBigDecimal,4,BigDecimal.ROUND_HALF_UP);
        return divide.multiply(new BigDecimal(100));
    }
}
